package values.tech
{
import flash.events.EventDispatcher;

import values.avatar.IPropertyProvider;
import values.events.TechValueEvent;

[Event(name="levelUp", type="values.events.TechValueEvent", field="act_rate")]
[Event(name="unlock", type="values.events.TechValueEvent", field="act_rate")]
/**
 * TechValue
 * @author jliang
 *
 */
public class TechValue extends EventDispatcher implements IPropertyProvider
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function TechValue()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _template:TechTemplateValue;

	public function get template():TechTemplateValue
	{
		return _template;
	}
	
	public function get key():String
	{
		return template.key;
	}
	
	public function get index():int
	{
		return template.index;
	}
	
	protected var _level:int;

	public function get level():int
	{
		return _level;
	}
	
	public function get currentLevelValue():TechLevelValue
	{
		return template.getLevel(level);
	}
	
	protected var _isUnlocked:Boolean = false;

	public function get isUnlocked():Boolean
	{
		return _isUnlocked;
	}
	
	[Prop(mapping="maxHP", field="max_hp")]
	public function get maxHP():Number
	{
		return currentLevelValue.getNumbericParam("max_hp");
	}
	
	[Prop(mapping="initMP", field="init_mp")]
	public function get initMP():Number
	{
		return currentLevelValue.getNumbericParam("init_mp");
	}
	
	[Prop(mapping="mpRecover", field="mp_recover")]
	public function get mpRecover():Number
	{
		return currentLevelValue.getNumbericParam("mp_recover");
	}
	
	[Prop(mapping="normalPower", field="normal_power")]
	public function get normalPower():Number
	{
		return currentLevelValue.getNumbericParam("normal_power");
	}
	
	[Prop(mapping="skillPower", field="skill_power")]
	public function get skillPower():Number
	{
		return currentLevelValue.getNumbericParam("skill_power");
	}
	
	[Prop(mapping="elementPower", field="element_power")]
	public function get elementPower():Number
	{
		return currentLevelValue.getNumbericParam("element_power");
	}
	
	[Prop(mapping="normalDefense", field="normal_defense")]
	public function get normalDefense():Number
	{
		return currentLevelValue.getNumbericParam("normal_defense");
	}
	
	[Prop(mapping="skillDefense", field="skill_defense")]
	public function get skillDefense():Number
	{
		return currentLevelValue.getNumbericParam("skill_defense");
	}
	
	[Prop(mapping="elementDefense", field="element_defense")]
	public function get elementDefense():Number
	{
		return currentLevelValue.getNumbericParam("element_defense");
	}
	
	[Prop(mapping="normalDamage", field="normal_damage")]
	public function get normalDamage():Number
	{
		return currentLevelValue.getNumbericParam("normal_damage");
	}
	
	[Prop(mapping="skillDamage", field="skill_damage")]
	public function get skillDamage():Number
	{
		return currentLevelValue.getNumbericParam("skill_damage");
	}
	
	[Prop(mapping="elementDamage", field="element_damage")]
	public function get elementDamage():Number
	{
		return currentLevelValue.getNumbericParam("element_damage");
	}
	
	[Prop(mapping="moveCapability", field="move_capability")]
	public function get moveCapability():Number
	{
		return currentLevelValue.getNumbericParam("move_capability");
	}
	
	[Prop(mapping="hitRate", field="hit_rate")]
	public function get hitRate():Number
	{
		return currentLevelValue.getNumbericParam("hit_rate");
	}
	
	[Prop(mapping="dodgeRate", field="dodge_rate")]
	public function get dodgeRate():Number
	{
		return currentLevelValue.getNumbericParam("dodge_rate");
	}
	
	[Prop(mapping="penetrateRate", field="penetrate_rate")]
	public function get penetrateRate():Number
	{
		return currentLevelValue.getNumbericParam("penetrate_rate");
	}
	
	[Prop(mapping="resistRate", field="resist_rate")]
	public function get resistRate():Number
	{
		return currentLevelValue.getNumbericParam("resist_rate");
	}
	
	[Prop(mapping="enforceRate", field="enforce_rate")]
	public function get enforceRate():Number
	{
		return currentLevelValue.getNumbericParam("enforce_rate");
	}
	
	[Prop(mapping="blockRate", field="block_rate")]
	public function get blockRate():Number
	{
		return currentLevelValue.getNumbericParam("block_rate");
	}
	
	[Prop(mapping="blockEffect", field="block_effect")]
	public function get blockEffect():Number
	{
		return currentLevelValue.getNumbericParam("block_effect");
	}
	
	[Prop(mapping="toughness", field="toughness")]
	public function get toughness():Number
	{
		return currentLevelValue.getNumbericParam("toughness");
	}
	
	[Prop(mapping="critRate", field="crit_rate")]
	public function get critRate():Number
	{
		return currentLevelValue.getNumbericParam("crit_rate");
	}
	
	[Prop(mapping="critEffect", field="crit_effect")]
	public function get critEffect():Number
	{
		return currentLevelValue.getNumbericParam("crit_effect");
	}
	
	[Prop(mapping="defenseRate", field="defense_rate")]
	public function get defenseRate():Number
	{
		return currentLevelValue.getNumbericParam("defense_rate");
	}
	
	[Prop(mapping="suppressRate", field="suppress_rate")]
	public function get suppressRate():Number
	{
		return currentLevelValue.getNumbericParam("suppress_rate");
	}
	
	[Prop(mapping="suppressEffect", field="suppress_effect")]
	public function get suppressEffect():Number
	{
		return currentLevelValue.getNumbericParam("suppress_effect");
	}
	
	[Prop(mapping="actRate", field="act_rate")]
	public function get actRate():Number
	{
		return currentLevelValue.getNumbericParam("act_rate");
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function initialize(template:TechTemplateValue, level:int, isUnlocked:Boolean):void
	{
		this._template = template;
		this._level = level;
		this._isUnlocked = isUnlocked;
	}
	
	public function upgrade():void
	{
		var oldLvl:int = level;
		_level ++;
		
		var evt:TechValueEvent = new TechValueEvent(TechValueEvent.LEVEL_UP);
		evt.params = { tech:this, oldValue:oldLvl, newValue:level };
		this.dispatchEvent(evt);
	}
	
	public function unlock():void
	{
		_isUnlocked = true;
		_level = 1;
		
		var evt:TechValueEvent = new TechValueEvent(TechValueEvent.UNLOCK);
		evt.params = { tech:this };
		this.dispatchEvent(evt);
		
		evt = new TechValueEvent(TechValueEvent.LEVEL_UP);
		evt.params = { tech:this, oldValue:0, newValue:1 };
		this.dispatchEvent(evt);
	}
	
	public function getLevelProperty(lvl:int, propField:String):Number
	{
		return template.getLevel(lvl).getNumbericParam(propField);
	}
}
}

